

            


            
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Xml;
namespace DatabaseComponent
{
     public partial class Courses
     {

         /// <summary>
         /// this function will get the directory of the course in our CourseFilesFolder
         /// </summary>
         /// <param name="objCourses">The course to get its path</param>
         /// <returns>a string containing the course path</returns>
         public static string getCoursePath(Courses objCourses)
         {
             HttpServerUtility Server = HttpContext.Current.Server;

             string strFileName = "./CourseFiles/" + objCourses.Path;

             FileInfo fi = new FileInfo(Server.MapPath(strFileName));

             return fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + "\\";

         }

         /// <summary>
         /// this function will search xmlnode childs for a child with specific name
         /// </summary>
         /// <param name="xmlParent">the parent xmlnode to search in its childs</param>
         /// <param name="strName">the name of the child to find</param>
         /// <returns>the first child found with that name</returns>
         public static XmlNode getChildWithName(XmlNode xmlParent, string strName)
         {
             XmlNode ret = null;
             foreach (XmlNode obj in xmlParent.ChildNodes)
             {
                 if (obj.Name.ToLower() == strName.ToLower())
                 {
                     ret = obj;
                     break;
                 }
             }

             return ret;
         }



         /// <summary>
         /// this function will get the meta data for any course if it's found
         /// </summary>
         /// <param name="objCourses">the course to get its meta data</param>
         /// <returns>a string array with title and description </returns>
         public static string [] getCourseMetaData(Courses objCourses)
         {



            string xmlName =  getCoursePath(objCourses) + "imsmanifest.xml";
            


            List<string> lstRet = new List<string>();


            try
            {

                if (!File.Exists(xmlName))
                {
                    return lstRet.ToArray();
                }


                XmlDocument xmlNew = new XmlDocument();
                xmlNew.Load(xmlName);

                XmlNode xn = null;
                xn = getChildWithName(xmlNew, "manifest");

                if (xn != null)
                {
                    XmlNode xmlParent = xn;

                    xn = getChildWithName(xmlParent, "organizations");


                    if (xn != null)
                    {
                        xmlParent = xn;
                        xn = getChildWithName(xmlParent, "organization");

                        if (xn != null)
                        {
                            xmlParent = xn;
                            xn = getChildWithName(xmlParent, "title");
                            if (xn != null)
                            {
                                if (xn.ChildNodes.Count > 0)
                                {
                                    lstRet.Add(xn.ChildNodes[0].Value);
                                }
                            }

                            xn = getChildWithName(xmlParent, "metadata");

                            if (xn != null)
                            {
                                xmlParent = xn;
                                xn = getChildWithName(xmlParent, "adlcp:location");
                                if (xn != null)
                                {
                                    if (xn.ChildNodes.Count > 0)
                                    {
                                        string fileName = xn.ChildNodes[0].Value;
                                        fileName = fileName.Replace("/", "\\");
                                        fileName = getCoursePath(objCourses) + fileName;
                                        if (!File.Exists(fileName))
                                        {
                                            return lstRet.ToArray();
                                        }



                                        xmlNew = new XmlDocument();
                                        xmlNew.Load(fileName);

                                        xn = getChildWithName(xmlNew, "lom");

                                        if (xn != null)
                                        {
                                            xmlParent = xn;
                                            xn = getChildWithName(xmlParent, "general");
                                            if (xn != null)
                                            {
                                                xmlParent = xn;
                                                xn = getChildWithName(xmlParent, "title");

                                                if (xn != null)
                                                {
                                                    XmlNode titleParent = xn;
                                                    xn = getChildWithName(titleParent, "langstring");
                                                    if (xn != null)
                                                    {
                                                        if (xn.ChildNodes.Count > 0)
                                                        {
                                                            if (lstRet.Count == 0)
                                                                lstRet.Add(xn.ChildNodes[0].Value);
                                                            else
                                                                lstRet[0] = xn.ChildNodes[0].Value;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (titleParent.ChildNodes.Count > 0)
                                                        {
                                                            if (titleParent.ChildNodes[0].NodeType == XmlNodeType.Text)
                                                            {
                                                                if (lstRet.Count == 0)
                                                                    lstRet.Add(titleParent.ChildNodes[0].Value);
                                                                else
                                                                    lstRet[0] = titleParent.ChildNodes[0].Value;

                                                            }
                                                        }
                                                    }
                                                }

                                                xn = getChildWithName(xmlParent, "description");

                                                if (xn != null)
                                                {
                                                    XmlNode descParent = xn;
                                                    xn = getChildWithName(descParent, "langstring");
                                                    if (xn != null)
                                                    {
                                                        if (xn.ChildNodes.Count > 0)
                                                        {

                                                            lstRet.Add(xn.ChildNodes[0].Value);

                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (descParent.ChildNodes.Count > 0)
                                                        {
                                                            if (descParent.ChildNodes[0].NodeType == XmlNodeType.Text)
                                                            {

                                                                lstRet.Add(descParent.ChildNodes[0].Value);


                                                            }
                                                        }
                                                    }
                                                }


                                            }
                                        }



                                    }
                                }
                            }
                        }


                    }


                }

            }
            catch
            {
            }

            return lstRet.ToArray();
         }


         public string strLanguageNameAR
         {
             get
             {


                 return objLanguages.LanguageNameAR;
             }

         }


         public string strLanguageNameEN
         {
             get
             {


                 return objLanguages.LanguageNameEN;
             }

         }
        

         public string strFolderNameAR
         {
             get {


                 return objFolder.objMostParent.FolderNameAR;
             }
             
         }


         public string strFolderNameEN
         {
             get {


                 return objFolder.objMostParent.FolderNameEN;
             }
             
         }


         Folders _objFolder = null;

         public Folders objFolder
         {
             get {
                 if (_objFolder == null)
                 {
                     FoldersDB objFoldersDB = new FoldersDB(ConnectionStrings.LMS);
                     List<Folders> lstFolders = objFoldersDB.getFoldersByID(FolderID);
                     if (lstFolders.Count == 0)
                         return new Folders();

                     _objFolder = lstFolders[0];
                 }
                 
                 
                 
                 
                 
                 return _objFolder; }
             
         }




         List<Questions> _lstQuestions = null;

         public List<Questions> lstQuestions
         {
             get {

                 if (_lstQuestions == null)
                 {
                     QuestionsDB objQuestionsDB = new QuestionsDB(ConnectionStrings.LMS);
                     _lstQuestions = objQuestionsDB.getQuestionsByCoursesID(ID);

                 }
                 
                 
                 
                 
                 
                 
                 return _lstQuestions; }
            
         }


         Languages _objLanguages = null;

         public Languages objLanguages
         {
             get {
                 if (_objLanguages == null)
                 {
                     LanguagesDB objDB = new LanguagesDB(ConnectionStrings.LMS);
                     List<Languages> lst = objDB.getLanguagesByID(LanguageID);
                     if (lst.Count == 0)
                         return new Languages();
                     _objLanguages = lst[0];
                 }
                             
                                
                 return _objLanguages; }
             
         }



         CoursesDegree _objCoursesDegree = null;

         public CoursesDegree objCoursesDegree
         {
             get {


                 CoursesDegreeDB objCoursesDegreeDB = new CoursesDegreeDB(ConnectionStrings.LMS);
                 List<CoursesDegree> lstCoursesDegree = objCoursesDegreeDB.getCoursesDegreeByCoursesID(ID);
                 CoursesDB objCoursesDB = new CoursesDB  (ConnectionStrings.LMS);
                 List<Courses> lstCurrentCourse = objCoursesDB.getCoursesByID (ID);
                 
                 if (lstCoursesDegree.Count == 0 && lstCurrentCourse.Count > 0)
                 {
                     CoursesDegree obj = new CoursesDegree();
                     obj.CoursesID = lstCurrentCourse[0].ID;
                     obj.Degree = 75;

                     objCoursesDegreeDB.Insert(obj);
                     lstCoursesDegree = objCoursesDegreeDB.getCoursesDegreeByCoursesID(ID);
                     
                 }

                 if (lstCoursesDegree.Count == 0)
                     _objCoursesDegree = new CoursesDegree();
                 else
                     _objCoursesDegree = lstCoursesDegree[0];
                   
                
                 
                 
                 return _objCoursesDegree; }
             
         }
     





            //Write any additional methods or properties here 
            //Don't modify any genrated classes
     }
}
